package com.molichuxing.framework.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiExcelUtils {
    public PoiExcelUtils() {
    }

    public static XSSFWorkbook createXSSFWorkbook(List<String> titles, String sheetStr) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet(sheetStr);
        XSSFRow titleRow = sheet.createRow(0);

        for(int cellIndex = 0; cellIndex < titles.size(); ++cellIndex) {
            XSSFCell cell = titleRow.createCell(cellIndex);
            cell.setCellValue((String)titles.get(cellIndex));
        }

        return workbook;
    }

    public static XSSFWorkbook createXSSFWorkbook(List<String> titles) {
        return createXSSFWorkbook(titles, "sheet0");
    }

    public static XSSFWorkbook createXSSFWorkbook(List<String> titles, List<List<String>> params, String password) throws Exception {
        XSSFWorkbook workbook = null;
        ByteArrayOutputStream baos = null;
        OPCPackage opc = null;

        try {
            workbook = createXSSFWorkbook(titles, params);
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            baos.flush();
            POIFSFileSystem fs = new POIFSFileSystem();
            EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
            Encryptor enc = info.getEncryptor();
            enc.confirmPassword(password);
            ByteArrayInputStream workbookinput = new ByteArrayInputStream(baos.toByteArray());
            opc = OPCPackage.open(workbookinput);
            OutputStream os = enc.getDataStream(fs);
            opc.save(os);
            opc.close();
            baos = new ByteArrayOutputStream();
            fs.writeFilesystem(baos);
        } finally {
            if (baos != null) {
                baos.close();
            }

            if (opc != null) {
                opc.close();
            }

        }

        return workbook;
    }

    public static XSSFWorkbook createXSSFWorkbook(List<String> titles, List<List<String>> params) {
        XSSFWorkbook workbook = createXSSFWorkbook(titles);
        XSSFSheet sheet = workbook.getSheetAt(0);

        for(int rowIndex = 1; rowIndex <= params.size(); ++rowIndex) {
            XSSFRow row = sheet.createRow(rowIndex);
            List<String> cellList = (List)params.get(rowIndex - 1);

            for(int cellIndex = 0; cellIndex < cellList.size(); ++cellIndex) {
                row.createCell(cellIndex).setCellValue((String)cellList.get(cellIndex));
            }
        }

        return workbook;
    }
}